package Hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import pojo.ST;

import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author 0624 苏昳云 Esther
 * @date 2022/2/9 9:09
 */
public class Enrollment_Date {

    public List<ST> date(String date) throws IOException {

        Configuration conf=HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.239.200");

        List<ST> list=new ArrayList<>();
        HTable table=new HTable(conf,"student");
        Scan scan=new Scan();
        Filter filter1=new SingleColumnValueFilter(Bytes.toBytes("status"),Bytes.toBytes("Enrollment_Date"),
                CompareFilter.CompareOp.EQUAL,new SubstringComparator(date));
        scan.setFilter(filter1);
        System.out.println(filter1);
        ResultScanner scanner=table.getScanner(scan);

        for(Result res:scanner){
            ST studentTable=new ST();
            String ID = Bytes.toString(res.getRow());
            String name = Bytes.toString(res.getValue(Bytes.toBytes("info"), Bytes.toBytes("Name")));
            String dateOfEnrollment = Bytes.toString(res.getValue(Bytes.toBytes("status"), Bytes.toBytes("Enrollment_Date")));
            String contactNumber = Bytes.toString(res.getValue(Bytes.toBytes("info"), Bytes.toBytes("ContactNumber")));
            String semester = Bytes.toString(res.getValue(Bytes.toBytes("status"), Bytes.toBytes("Semester")));
            String studentStatus = Bytes.toString(res.getValue(Bytes.toBytes("status"), Bytes.toBytes("StudentStatus")));

            studentTable.setStudentID(ID);
            studentTable.setStudentSemester(semester);
            studentTable.setStudentNumber(contactNumber);
            studentTable.setStudentName(name);
            studentTable.setStudentStatus(studentStatus);
            studentTable.setStudentDate(dateOfEnrollment);


            System.out.println(studentTable.getStudentID());
            list.add(studentTable);
            System.out.println(Arrays.toString(list.toArray()));
        }
        scanner.close();
        return list;
    }
}